<?php
/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/6/3
 * Time: 14:34
 */

namespace app\vehicle\logic;
use app\vehicle\model\Bus;
use app\vehicle\model\BusSkd;
class busLogic
{
    //获取首页订单列表
    public function getBusList($data){
        $time = strtotime(date('Y-m-d'));
        $where[] = ['skd_date','egt',$time];
        $data['begin_city']>0 && $where[] = ['begin_city','eq',$data['begin_city']];
        $data['end_city']>0 && $where[] = ['end_city','eq',$data['end_city']];
        $data['skd_time'] && $where[] = ['skd_date','egt',strtotime($data['skd_time'])];
        $list = BusSkd::with('bus')->where($where)->group('bus_id')->page($data['page'],10)->order('update_time desc')->select();
        return $list;
    }
    //获取车辆详情
    public function getBusInfo($id){
        $info = Bus::with(['bus_business','bus_driver','order_comment','bus_img'])->where('id','eq',$id)->find();
        return $info;
    }
    //获取车辆的空闲时间
    public function getBusTime($bus_id){
        $time = time();
        $where = [
            ['end_date','egt',$time],
            ['bus_id','eq',$bus_id],
            ['status','lt',2]
        ];
        $list = BusSkd::where($where)->field('skd_date,end_date')->select();
        $arr = [];
        foreach($list as $k=>$v){
            if($v['skd_date']<$time && $v['end_date']>$time) {
                $date = '今日';
            }else if($v['skd_date']<$time+86400 && $v['end_date']>$time+86400) {
                $date = '明日';
            }else{
                $date = date('m-d', $v['skd_date']);
            }
            $arr[$k]['str'] = $date;
        }
        return $arr;
    }
    //判断车辆条件是否满足
    public function checkBusInfo($data){
        $date = strtotime($data['date']);
        $end_date = $date+$data['number']*86400;
        $check = BusSkd::whereStatus('lt',2)->whereBusId('eq',$data['id'])->whereBetween('end_date',[$date,$end_date])->count();
       if($check==$data['number']){
           return 1;
       }else{
           return '车辆天数不足';
       }
    }
    //获取车辆投递信息
    public function getPerfechInfo($id){
        $info = BusSkd::with(['start','end'])->where('id','eq',$id)->find();
        return $info;
    }
}